import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Bk_Junior_College } from 'src/junior_college/bk_junior_college.entity';
import { DeleteResult, Repository, UpdateResult } from 'typeorm';
import { Bk_Discipline } from './bk_discipline.entity';

@Injectable()
export class DisciplineService {
    constructor(
        @InjectRepository(Bk_Discipline)
        private readonly disciplineRepository: Repository<Bk_Discipline>
    ) { }

    async add(discipline: any): Promise<Bk_Discipline> {
        return await this.disciplineRepository.save(discipline);
    }

    async delete(id: number): Promise<DeleteResult> {
        return await this.disciplineRepository.delete(id);
    }

    async update(id: number, discipline: any): Promise<UpdateResult> {
        return await this.disciplineRepository.update(id, discipline);
    }

    async findAll(query: any): Promise<[Bk_Discipline[], number]> {
        return await this.disciplineRepository.findAndCount({ relations: ["junior_college"], skip: query.skip, take: query.take });
    }

    async getDisciplinesByCollege(juniorCollege: { id: number, college_name: string }): Promise<Bk_Discipline[]> {
        return await this.disciplineRepository.find({ relations: ["junior_college"], where: { junior_college: juniorCollege } });
    }
}